package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private JedisPool jedisPool;
    @Reference
    private MemberService memberService;

    @Reference

    //查询客户端个人中心
    @RequestMapping("/findMemberDetails")
    public Result findMemberDetails(HttpServletRequest request) {

        boolean flag = judgeLogin(request);
        Jedis resource = null;
        try {
            if (flag == false) {
                return new Result(false, MessageConstant.LOGIN_STATE);
            } else {
                ObjectMapper mapper = new ObjectMapper();

                //查询cookie 确认是否登录
                Cookie[] cookies = request.getCookies();
                for (Cookie cookie : cookies) {
                    if ("telephone".equals(cookie.getName())) {
                        String value = cookie.getValue();
                        resource = jedisPool.getResource();
                        //通过cookie查询出保存在redis的json字符串
                        String memberJson = resource.get(value);
                        if (memberJson != null) {
                            //通过objectMapper把json转换成对象
                            Member member = mapper.readValue(memberJson, Member.class);
                            //把数据传递给前端界面展示
                            return new Result(true, MessageConstant.GET_PERSONAL_INFORMATION_SUCCESS, member);
                        }
                    }

                }

            }
            return new Result(false, MessageConstant.LOGIN_STATE);

        } catch (Exception e) {
            e.printStackTrace();
            //未登录 直接返回false
            return new Result(false, MessageConstant.EDIT_PWD_FAIL1);
        } finally {
            if (resource != null) {
                resource.close();
            }

        }

    }

    //修改客户端个人信息
    @RequestMapping("/updateMemberDetail")
    public Result updateMemberDetail(@RequestBody Member member) {
        Jedis resource = null;
        try {
            memberService.updateMemberDetail(member);
            String telephone = member.getPhoneNumber();
            //将会员信息转换为json类型
            String json = JSON.toJSON(member).toString();
            //将会员信息添加到redis中
            resource = jedisPool.getResource();
            resource.setex(telephone, 60 * 30, json);

            return new Result(true, MessageConstant.EDIT_PERSONAL_INFORMATION_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERSONAL_INFORMATION_FAIL);
        } finally {
            if (resource != null) {
                resource.close();
            }
        }

    }

    //通过日期查询该用户所有预约信息
    @RequestMapping("/findOrderRecord")
    public Result findOrderRecord(@RequestParam String startTime, @RequestParam String endTime, HttpServletRequest request) {
        try {
            boolean flag = judgeLogin(request);
            if (flag == false) {
                return new Result(false, MessageConstant.LOGIN_STATE);
            } else {
                if (startTime == "" || endTime == "") {
                    return new Result(false, "请选择正确的查询日期");
                }
                Map map = new HashMap();
                Date start = DateUtils.parseString2Date(startTime);
                Date end = DateUtils.parseString2Date(endTime);
                map.put("startTime", start);
                map.put("endTime", end);
                Cookie[] cookies = request.getCookies();
                if (cookies != null) {
                    for (Cookie cookie : cookies) {
                        if ("telephone".equals(cookie.getName())) {
                            String telephone = cookie.getValue();
                            map.put("phoneNume", telephone);
                            List<Map> list = memberService.findByTelAndOrderDate(map);
                            if (list != null) {
                                return new Result(true, MessageConstant.GET_ALL_ORDER_SUCCESS, list);
                            }
                            return new Result(true, MessageConstant.GET_ALL_ORDER_NULL);
                        }
                    }
                }
            }

            return new Result(false, MessageConstant.LOGIN_STATE);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PWD_FAIL1);
        }

    }

    //查询用户的所有预约记录
    @RequestMapping("/findOrderByTel")
    public Result findOrderByTel(HttpServletRequest request) {
        try {
            boolean flag = judgeLogin(request);
            if (flag == false) {
                return new Result(false, MessageConstant.LOGIN_STATE);
            } else {
                Cookie[] cookies = request.getCookies();
                if (cookies != null) {
                    for (Cookie cookie : cookies) {
                        if ("telephone".equals(cookie.getName())) {
                            String telephone = cookie.getValue();
                            List<Map> list = memberService.findOrderByTel(telephone);
                            if (list != null) {
                                return new Result(true, MessageConstant.GET_ALL_ORDER_SUCCESS, list);
                            }
                            return new Result(true, MessageConstant.GET_ALL_ORDER_NULL);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.LOGIN_STATE);
        }
        return new Result(false, MessageConstant.LOGIN_STATE);
    }

    //判断是否登录
    public boolean judgeLogin(HttpServletRequest request) {
        boolean flag = false;
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("telephone".equals(cookie.getName())) {
                    flag = true;
                }
            }
        }
        return flag;

    }
}
